/**
 * 字典管理
 * @constructor
 */
function Dict() {
}

Dict.init = function () {
    let vm = new Vue({
        el: "#customModule",
        data: {
            //实体
            immuneID:"",
            uID:"",
            immuneTime:"",
            number:"",
            vaccineName:"",
            manufacturers:"",
            batchNumber:"",
            validity:"",
            createTime:"",
            dose:"",
            methods:"",
            remark:"",
            //搜索值
            enterpriseNameCondition: "",
            //附加属性
            clickState: "",
            roleID: "",
            isAdd: "true",
            roleList: ""
        },
        created() {
            //日期区间选择
            laydate.render({
                elem: '#immuneTime',
                theme: 'molv',
                type:'date',
                done:function (value) {
                    vm.immuneTime = value;
                }
            });
            laydate.render({
                elem: '#validity',
                theme: 'molv',
                type:'date',
                done:function (value) {
                    vm.validity = value;
                }
            });
            this.roleID = $.cookie('RoleIds');
            self = this;
            window.changeState = self.changeState;
            window.edit = self.edit;
            window.del = self.del;
        },
        mounted() {
            this.initBootStrapSource();
        },
        methods: {
            initBootStrapSource() {
                $('#tableList').bootstrapTable({
                    url: '/dict/tableSearch',
                    method: 'post',
                    toolbar: "#toolbar",
                    sidePagination: 'server',
                    dataField: "data",
                    striped: true,
                    pagination: true,
                    pageNumber: 1,
                    pageSize: 10,
                    pageList: [10, 25, 50, 100],
                    sortable: true,
                    sortName: 'createDateTime',
                    sortOrder: "desc",
                    search: false,
                    showRefresh: true,
                    showColumns: true,
                    showExport: true,
                    showToggle: true,
                    queryParamsType: 'undefined',
                    queryParams: function queryParams(params) {
                        return {
                            pageSize: params.pageSize,      //页面大小
                            pageIndex: params.pageNumber,   //页码
                            sort: params.sortName,      //排序列名
                            sortOrder: params.sortOrder, //排位命令（desc，asc）
                        };
                    },
                    onLoadSuccess: function (data) {
                        //Help.alertSuccMessage("数据加载完毕");
                    },
                    onLoadError: function (status) {
                        // Help.alertErrorMessage("数据加载出错");
                    },
                    columns: [
                        {
                            title: '序号',//标题  可不加
                            align: "center",
                            width: 40,
                            sortable: true,
                            formatter: function (value, row, index) {
                                var pageSize = $('#tableList').bootstrapTable('getOptions').pageSize;//通过表的#id 可以得到每页多少条
                                var pageNumber = $('#tableList').bootstrapTable('getOptions').pageNumber;//通过表的#id 可以得到当前第几页
                                return pageSize * (pageNumber - 1) + index + 1;//返回每条的序号： 每页条数 * （当前页 - 1 ）+ 序号
                            }
                        },
                        {field: 'tagName', title: '标签名', align: 'center'},
                        {field: 'dataValue', title: '数据值', align: 'center'},
                        {field: 'type', title: '类型', align: 'center'},
                        {field: 'typeDescription', title: '类型描述', align: 'center'},
                        {field: 'sort', title: '排序', align: 'center'},
                        {field: 'createTime', title: '创建时间', align: 'center'},
                        {field: 'remark', title: '备注', align: 'center'},
                        {
                            title: '操作', align: 'center',
                            formatter: function (value, row, index) {
                                console.log("row",row)
                                let optionHtml;
                                for (let i = 0; i < row.buttonList.length; i++) {
                                    let button = row.buttonList[i];
                                    if (button.enDesc === "Delete")
                                        optionHtml = optionHtml + " <a class=\"btn btn-danger btn-xs\" href=\"javascript:void(0)\"  onclick='del(" + JSON.stringify(row) + ")' title=\"删除\"><i class=\"fa fa-remove\"></i>删除</a>";
                                    else if (button.enDesc === "Update")
                                        optionHtml = "<a class=\"btn btn-success btn-xs\"  href=\"javascript:void(0)\"  onclick='edit(" + JSON.stringify(row) + ")'  title=\"修改\" ><i class=\"fa fa-edit\"></i>修改</a>";
                                    else vm.isAdd = button.enDesc === "Insert";
                                }
                                return optionHtml;
                            }
                        }
                    ]
                });
            },
            /**
             * 查询
             */
            search() {
                vm.refresh();
            },
            /**
             * 重置
             */
            reset() {
                vm.enterpriseNameCondition = "";
                $("#enterpriseNameCondition").val("");
                vm.refresh();
            },
            /**
             * 编辑
             * @param data
             */
            edit(data) {
                vm.immuneID = data.immuneID;
                vm.uID = data.uID;
                vm.immuneTime = data.immuneTime;
                vm.number = data.number;
                vm.vaccineName = data.vaccineName;
                vm.manufacturers = data.manufacturers;
                vm.batchNumber = data.batchNumber;
                vm.validity = data.validity;
                vm.createTime = data.createTime;
                vm.dose = data.dose;
                vm.methods = data.methods;
                vm.remark = data.remark;
                vm.clickState = "edit";
                $('#myModalAdd').modal('show');
            },
            /**
             * 删除
             * @param data
             */
            del(data) {
                swal({
                    title: "确认是否删除",
                    text: "是否确认要删除该条记录？",
                    type: "warning",
                    showCancelButton: true,
                    confirmButtonColor: "#DD6B55",
                    confirmButtonText: "删除",
                    cancelButtonText: "取消",
                    closeOnConfirm: false
                }, function () {
                    let url = "/immune/del";
                    let pm = {
                        immuneID: data.immuneID
                    };
                    axios.post(url, pm).then(data => {
                        if (data.data.code === "200") {
                            swal("删除成功!", "请继续操作", "success");
                            vm.refresh();
                        } else {
                            swal("删除失败!", "请重试", "error");
                        }
                    });
                });
            },
            /**
             * 新增
             */
            addRecord() {
                vm.immuneID = "";
                vm.uID = "";
                vm.immuneTime = "";
                vm.number = "";
                vm.vaccineName = "";
                vm.manufacturers = "";
                vm.batchNumber = "";
                vm.validity = "";
                vm.createTime = "";
                vm.dose = "";
                vm.methods = "";
                vm.remark = "";
                vm.clickState = "add";
                $('#myModalAdd').modal('show');
            },
            /**
             * 保存
             */
            saveRecord() {
                let pm = {
                    immuneID:vm.immuneID,
                    uID:vm.uID,
                    immuneTime:vm.immuneTime,
                    number:vm.number,
                    vaccineName:vm.vaccineName,
                    manufacturers:vm.manufacturers,
                    batchNumber:vm.batchNumber,
                    validity:vm.validity,
                    createTime:vm.createTime,
                    dose:vm.dose,
                    methods:vm.methods,
                    remark:vm.remark,
                }
                console.log("pm",pm)
                if (vm.clickState === "edit") {
                    let url = "/immune/edit";
                    axios.post(url, pm).then(data => {
                        if (data.data.code === "200") {
                            $('#myModalAdd').modal('hide')
                            swal("修改成功!", "请继续操作", "success");
                            this.refresh();
                        }
                    }).catch(e => {
                        console.log(e);
                        swal("修改失败!", "请重试", "error");
                    });
                } else if (vm.clickState === "add") {
                    let url = "/immune/add";
                    axios.post(url, pm).then(data => {
                        $('#tableList').bootstrapTable(('refresh'));
                        if (data.data.code === "200") {
                            $('#myModalAdd').modal('hide')
                            swal("添加成功!", "请继续操作", "success");
                            this.refresh();
                        }
                    }).catch(e => {
                        console.log(e);
                        swal("添加失败!", "请重试", "error");
                    });
                }
            },
            refresh() {
                $('#tableList').bootstrapTable(('refresh'));
            }
        }
    });
};